package com.tianjian.myblog.service;

import com.tianjian.myblog.dao.TagRepository;
import com.tianjian.myblog.exception.NotFoundException;
import com.tianjian.myblog.model.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository repository;
    @Override
    public Tag saveTag(Tag tag) {
        return repository.save(tag);
    }

    @Override
    public Tag findOneTag(Long id) {

        return repository.findOne(id);
    }

    @Override
    public Tag findByName(String name) {

        return repository.findByName(name);
    }

    @Override
    public Page<Tag> findTag(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public Tag updateTag(Long id, Tag tag) {

        Tag t = repository.findOne(id);
        if(t==null) {
            throw new NotFoundException("标签不存在");
        }

        BeanUtils.copyProperties(tag,t);


        return repository.save(t);
    }

    @Override
    public void deleteTag(Long id) {

        repository.delete(id);
    }
}
